{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d7a49647",
   "metadata": {},
   "source": [
    "# 1.语音录制\n",
    "\n",
    "* 1. 安装和下载模块：pip install 模块名称\n",
    "> 1. SpeechRecognition\n",
    "> 2. Pyaudio\n",
    "> 3. pydub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c28a7ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "# r.recognition_timeout = 3\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source,phrase_time_limit=3)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c146126a",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"你好\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2930c59a",
   "metadata": {},
   "source": [
    "# 2.语音识别（百度的ASR）auto speech recognition\n",
    "\n",
    "* I/O: 音频文件-->文本文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a66222ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "31f4dfb9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"refresh_token\":\"25.09d6070fef85fa986a54f11980c4fb67.315360000.1999863980.282335-33473708\",\"expires_in\":2592000,\"session_key\":\"9mzdC34WOarKh4\\/qH\\/klY0quSDzxjm7NlRTKzS4HHAyELNGaI9O7HXBDt2Te9aGbSjEaBMVa3hVtT7DifSaKG5\\/zgc98+g==\",\"access_token\":\"24.0fb8910b315f004e18778d31f0cb2d83.2592000.1687095980.282335-33473708\",\"scope\":\"audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\\u6743\\u9650 vis-classify_flower lpq_\\u5f00\\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\\u5f00\\u653eScope vis-ocr_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 idl-video_\\u865a\\u62df\\u4eba\\u7269\\u52a9\\u7406 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx\",\"session_secret\":\"cf814190e047ff628145fce6d0738fe0\"}\n",
      "\n",
      "{'refresh_token': '25.09d6070fef85fa986a54f11980c4fb67.315360000.1999863980.282335-33473708', 'expires_in': 2592000, 'session_key': '9mzdC34WOarKh4/qH/klY0quSDzxjm7NlRTKzS4HHAyELNGaI9O7HXBDt2Te9aGbSjEaBMVa3hVtT7DifSaKG5/zgc98+g==', 'access_token': '24.0fb8910b315f004e18778d31f0cb2d83.2592000.1687095980.282335-33473708', 'scope': 'audio_voice_assistant_get brain_enhanced_asr audio_tts_post brain_speech_realtime public brain_all_scope brain_asr_async wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': 'cf814190e047ff628145fce6d0738fe0'}\n",
      "audio_voice_assistant_get\n",
      "SUCCESS WITH TOKEN: 24.0fb8910b315f004e18778d31f0cb2d83.2592000.1687095980.282335-33473708  EXPIRES IN SECONDS: 2592000\n",
      "Request time cost 0.833050\n",
      "{\"corpus_no\":\"7234889505275760190\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"你好，我叫张亮。\"],\"sn\":\"406682640351684503980\"}\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'你好，我叫张亮。'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bd_asr_result = bdasr.asr('1.wav')\n",
    "bd_asr_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2561f45a",
   "metadata": {},
   "source": [
    "# 3. 自然语言处理（NLP）\n",
    "\n",
    "* I/O: 文本-->文本（文本的生成： 文本的理解，文本的处理，文本的生成）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c2e15ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa ={\n",
    "    '你好。':\"你好，欢迎使用自然语言处理，很高兴为你服务\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14b33194",
   "metadata": {},
   "outputs": [],
   "source": [
    "import opchat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dee70f6f",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = opchat.openai_chat(bd_asr_result)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a17504b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# result = qa.get(bd_ars_result)\n",
    "# result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c040bdf0",
   "metadata": {},
   "source": [
    "# 4. 语音合成（百度的TTS）text to speech\n",
    "\n",
    "* I/O: 文本文件-->音频文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "357ef6fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdtts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d759c0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "bdtts.tts(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a1cf36f",
   "metadata": {},
   "source": [
    "# 5. 播放语音合成的音频"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a24c0cbe",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa46c0b6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f61e6f15",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
